package com.qf.mao.servlet.grade;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.mao.entity.GradeDTO;
import com.qf.mao.entity.RespBean;
import com.qf.mao.service.GradeService;
import com.qf.mao.utils.CommonsUtils;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

//http://localhost:8080/s/grade?action=page
@SuppressWarnings("all")
@WebServlet("/GradeServlet")
public class GradeServlet extends HttpServlet {
    GradeService gradeService = new GradeService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        if ("page".equals(action)) {
            request.getRequestDispatcher("/WEB-INF/jsp/gradeList.jsp").forward(request, response);
        } else if ("data".equals(action)) {
            //获取 json
            //查询所有的年级
            List<GradeDTO> list = gradeService.getAllGrades();
            response.setContentType("application/json;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.write(new ObjectMapper().writeValueAsString(list));
        }else if ("data_from_clazz".equals(action)) {
            //获取 json
            //查询所有的年级
            List<GradeDTO> list = gradeService.getAllGrades();
            GradeDTO gradeDTO = new GradeDTO();
            gradeDTO.setGid(-1);
            gradeDTO.setGradeName("全部");
            list.add(0, gradeDTO);
            response.setContentType("application/json;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.write(new ObjectMapper().writeValueAsString(list));
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取年级名字
        String gradeName = request.getParameter("gradeName");
        //获取课程id cid
        String[] cids = request.getParameterValues("cid");
        //需要的是数字 把String转换成int
        List<Integer> collect = Arrays.stream(cids).map(i -> Integer.parseInt(i)).collect(Collectors.toList());
        Integer result = gradeService.addGrade(gradeName, collect);
        RespBean respBean = null;
        if (result == CommonsUtils.ERROR_NAME) {
            respBean = RespBean.respError("年级名重复，添加失败");
        } else if (result == CommonsUtils.SUCCESS_OK) {
            respBean = RespBean.respSuccess("添加成功");
        } else {
            respBean = RespBean.respError("添加失败");
        }
            response.setContentType("application/json;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.write(new ObjectMapper().writeValueAsString(respBean));

    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String gid = req.getParameter("gid");
        Integer result = gradeService.deleteGradeByGid(Integer.parseInt(gid));
        RespBean respBean = null;
        if (result == CommonsUtils.SUCCESS_DELETE) {
            respBean = RespBean.respSuccess("删除成功");
        }else{
            respBean = RespBean.respError("删除失败");
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.write(new ObjectMapper().writeValueAsString(respBean));
    }
}
